﻿package com.lbynet.util{
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	public class Util {
		import flash.utils.ByteArray;
		public function Util() {
		}
		public static const SERVER:String = "";

		//时间显示(YYYYMMDD)
		public static function getDateStr(t:Number):String {
			var year:String; 
			var month:String;
			var date:String;
			var d:Date = new Date(t);
			year = ""+d.getFullYear();
			if (d.getMonth()<9) {
				month = "0"+(d.getMonth()+1);
			} else {
				month = ""+(d.getMonth()+1);
			}
			if (d.getDate()<10) {
				date = "0"+d.getDate();
			} else {
				date = ""+d.getDate();
			}
			return year+month+date;
		}
		
		//时间显示(YYMMDDHHMMSS)
		public static function getTime(t:Number):String {
			var year:String;
			var month:String;
			var date:String;
			var hour:String;
			var minute:String;
			var second:String;
			var d:Date = new Date(t);
			year = (""+d.getFullYear());
			month = d.getMonth()<9 ? "0"+(d.getMonth()+1):""+(d.getMonth()+1);
			date = d.getDate()<10 ? "0"+d.getDate():""+d.getDate();
			hour = d.getHours()<10 ? "0"+d.getHours() : ""+d.getHours();
			minute= d.getUTCMinutes()<10 ? "0"+d.getUTCMinutes() : ""+d.getUTCMinutes();
			second= d.getUTCSeconds()<10 ? "0"+d.getUTCSeconds() : ""+d.getUTCSeconds();
			return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
		}
		
		//时间显示(YYMMDDHHMMSS)
		public static function getTimeStr(t:Number):String {
			var year:String;
			var month:String;
			var date:String;
			var hour:String;
			var minute:String;
			var second:String;
			var d:Date = new Date(t);
			year = (""+d.getFullYear()).toString().substr(2,4);
			month = d.getMonth()<9 ? "0"+(d.getMonth()+1):""+(d.getMonth()+1);
			date = d.getDate()<10 ? "0"+d.getDate():""+d.getDate();
			hour = d.getHours()<10 ? "0"+d.getHours() : ""+d.getHours();
			minute= d.getUTCMinutes()<10 ? "0"+d.getUTCMinutes() : ""+d.getUTCMinutes();
			second= d.getUTCSeconds()<10 ? "0"+d.getUTCSeconds() : ""+d.getUTCSeconds();
			return year+month+date+hour+minute+second;
		}
		
		//格式化时间显示
		public static function formatTime(time:Number):String {
			var tempDate:Date = new Date(time);
			var tempHours:String = tempDate.getUTCHours()<10 ? "0"+tempDate.getUTCHours() : ""+tempDate.getUTCHours();
			var tempMinutes:String = tempDate.getUTCMinutes()<10 ? "0"+tempDate.getUTCMinutes() : ""+tempDate.getUTCMinutes();
			return tempHours.concat(":",tempMinutes);
		}
		//格式化时间显示formatTimeHMS
		public static function formatTimeHMS(time:Number):String {
			var tempDate:Date = new Date(time);
			var tempHours:String = tempDate.getUTCHours()<10 ? "0"+tempDate.getUTCHours() : ""+tempDate.getUTCHours();
			var tempMinutes:String = tempDate.getUTCMinutes()<10 ? "0"+tempDate.getUTCMinutes() : ""+tempDate.getUTCMinutes();
			var tempSeconds:String = tempDate.getUTCSeconds()<10 ? "0"+tempDate.getUTCSeconds() : ""+tempDate.getUTCSeconds();
			return tempHours.concat(":",tempMinutes,":",tempSeconds);
		}
		//格式化浮点数显示
		public static function formatNumber(num:Number):String {
			num +=0.00;
			var str:String = num.toFixed(2);
			if(str.charAt(str.length-1)=="0" && str.charAt(str.length-2)=="0")
				str = str.substring(0,str.length-3);
			return str;
		}
		//计算倒计时 HH:SS
		public static function countDown(t:Number):String {
			if(t>=0) {
				var hh:int = int(t/3600000);
				var mm:int = int((t%3600000)/60000);
				var ss:int = int((t%60000)/1000);
				var hhStr:String = hh<10?"0"+hh:""+hh;
				var mmStr:String = mm<10?"0"+mm:""+mm;
				var ssStr:String = ss<10?"0"+ss:""+ss;
				return hhStr.concat(":",mmStr,":",ssStr);
			}
			return "";
		}
		public static function readLong(byteArray:ByteArray):Number {
			if(byteArray.bytesAvailable >= 8) {
				var u1, u2:uint;
				u1=byteArray.readUnsignedInt();
				u2=byteArray.readUnsignedInt();
				var total:Number;
				total = Number(u1);
				total = total *4294967296;
				total += u2;
				//trace("取到时间为:"+new Date(total));
				return total;
			}
			else {
				trace("Function longToNumber ByteArray[8]长度不能为:"+byteArray.bytesAvailable);
				return 0;
			}
		}
		
		public static function writeLong(byteArray:ByteArray,longInt:Number):void {
			var t1, t2:uint;
			t1 =longInt;
			t2 = longInt/4294967296;
			byteArray.writeUnsignedInt(t2);
			byteArray.writeUnsignedInt(t1);
		}
		/**
		 *	关闭当前页面并打开一个新的URL
		 *	param url:需要打开的url
		 *	param type:
		 */
		public static function go(url:String):void {
			url="javascript:window.open('"+url+"','win"+ windowCounter++ +"');void(0);window.close();";
			trace(url);
			var request:URLRequest = new URLRequest(url);
			try {
				navigateToURL(request,"_self");
			} catch (e:Error) {
				// handle error here
			}
		}		
		
		/**
		 *	在新的浏览器中打开一个URL
		 *	param url:需要打开的url
		 *	param type:
		 */
		private static var windowCounter:int = 0;
		public static function openURL(url:String,type:String):void {
			url="javascript:window.open('"+url+"','win"+ windowCounter++ +"'); void(0);";
			trace(url);
			var request:URLRequest = new URLRequest(url);
			try {
				navigateToURL(request,"_self");
			} catch (e:Error) {
				// handle error here
			}
		}
		
		/**
		 *	在当前浏览器中打开一个URL
		 *	param url:需要打开的url
		 *	param type:
		 */
		public static function goto(url:String):void {
			url="javascript:location.reload('"+url+"');";
			trace(url);
			var request:URLRequest = new URLRequest(url);
			try {
				navigateToURL(request,"_self");
			} catch (e:Error) {
				// handle error here
			}
		}
		
		/**
		 *	重新加载
		 *	param url:需要打开的url
		 *	param type:
		 */
		public static function reload():void {
			var url:String="javascript:location.reload();";
			var request:URLRequest = new URLRequest(url);
			try {
				navigateToURL(request,"_self");
			} catch (e:Error) {
				// handle error here
			}
		}
	}
}